'''
dionysus - a web based dialogue management framework
Copyright (C) 2011  Eugeniu Costetchi <eugeniu.costetchi (at) tudor.lu>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Created on Dec 26, 2010
'''
from DialogueManager.Context import Context
from DialogueManager.planning.PlanFragment import Plan
from DialogueManager.protocols.explorative import all_fragments
from DialogueManager.planning.PlanExecutor import PlanExecutor
from DialogueManager.utils.log_utils import log_debug
from DialogueManager.utils.qaf_reader import get_qaf_array,\
    qaf_array_to_dict

class __SingletonDictionary(dict):
    pass
DialoguePool = __SingletonDictionary()

class Dialoguer():
    '''
    classdocs
    '''
    
    def __init__(self, dialogue_id=None):
        #construct a plan
        self.dialogue_plan = Plan()+all_fragments()
        #TODO: to be externalized
        
        #init context
        self.dialogue_context = Context()
        #insert qaf templates
        self.dialogue_context[Context.QAF_TEMPLATES] = qaf_array_to_dict(get_qaf_array())
        
        self.dialogue_plan_executor = PlanExecutor(self.dialogue_plan.graph,
                                                   self.dialogue_plan.begin_node,
                                                   self.dialogue_plan.end_node,
                                                   self.dialogue_context)

        log_debug('---------------------------------------------------------------new Dialoguer()')
        #log_debug('Constructed the plan with factory greeting resulting in stack:' + self.dialogue_plan_executor.print_current_stack())
    
    def listen(self, utterance):
        try:
            #self.dialogue_plan_executor.listen(utterance)
            self.dialogue_context.set_receive(utterance)
        except:
            log_debug("Exception during Listen operation", exception=True)

    def say(self):
        try:
            #need to pause here, synchronize with DM 
            #and then to reply to the answer
            
            cl = []
            x = self.dialogue_context.get_emit() #receive list of messages
            for i in x:
                cl.append(i)
            if x is None or len(x)==0: 
                cl.append('I have nothing to say. Either the dialogue plan is finished, or there was an internal error')
            return cl
        except:
            log_debug("Exception during Say operation", True)

    def process(self, consume_top_listen_action=False):
        try:
            self.dialogue_plan_executor.execute_until_break()
        except:
            log_debug("Exception during Process operation",True)
